package br.edu.unipampa.view;

import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;

import br.edu.unipampa.util.BaseScene;
import br.edu.unipampa.util.ResourcesManager;
import br.edu.unipampa.util.SceneManager.SceneType;

public class Menu extends BaseScene implements IOnMenuItemClickListener {
	private MenuScene menuChildScene;
	private final int MENU_CRIAR_PARTIDA = 0;
	private final int MENU_CONECTAR_PARTIDA = 1;
	private final int MENU_SAIR = 2;

	private void createMenuChildScene()
	{
	    menuChildScene = new MenuScene(camera);
	    menuChildScene.setPosition(300, 30);
	    
	    final IMenuItem playMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_CRIAR_PARTIDA, resourcesManager.textConectarPartida, vbom), 2, 1);
	    final IMenuItem optionsMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_CONECTAR_PARTIDA, resourcesManager.textCriarPartida, vbom), 2, 1
	    		);
	    final IMenuItem sair = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_SAIR, resourcesManager.textSair, vbom), 2, 1);
	    
	    menuChildScene.addMenuItem(playMenuItem);
	    menuChildScene.addMenuItem(optionsMenuItem);
	    menuChildScene.addMenuItem(sair);
	    
	    menuChildScene.buildAnimations();
	    menuChildScene.setBackgroundEnabled(false);
	    
	    playMenuItem.setPosition(playMenuItem.getX(), playMenuItem.getY() + 5);
	    optionsMenuItem.setPosition(optionsMenuItem.getX(), optionsMenuItem.getY() + 5);
	    sair.setPosition(sair.getX(), sair.getY() + 10);
	    
	    menuChildScene.setOnMenuItemClickListener(this);
	    
	    setChildScene(menuChildScene);
	}
	
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
	{
	        switch(pMenuItem.getID())
	        {
	        case MENU_CRIAR_PARTIDA:
	            return true;
	        case MENU_CONECTAR_PARTIDA:
	            return true;
	        default:
	            return false;
	    }
	}
	
	@Override
	public void createScene() {
		createBackground();
		createMenuChildScene();
	}

	@Override
	public void onBackKeyPressed() {
		System.exit(0);
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_MENU;
	}

	@Override
	public void disposeScene() {
		this.detachSelf();
		this.dispose();
	}

	private void createBackground() {
		SpriteBackground background = new SpriteBackground(new Sprite(0, 0, resourcesManager.textBackground, vbom));
		this.setBackground(background);
	}

}
